#!/bin/sh
#
#  COPYRIGHT 2004, EMULEX CORPORATION
#  3333 Susan Street, Costa Mesa, CA 92626
#  
#  All rights reserved.  This computer program and related documentation
#  is protected by copyright and distributed under licenses restricting
#  its use, copying, distribution and decompilation.  This computer
#  program and its documentation are CONFIDENTIAL and a TRADE SECRET
#  of EMULEX CORPORATION.  The receipt or possession of this program
#  or its documentation does not convey rights to reproduce or disclose
#  its contents, or to manufacture, use, or sell anything that it may
#  describe, in whole or in part, without the specific written consent
#  of EMULEX CORPORATION.  Any reproduction of this program without
#  the express written consent of EMULEX CORPORATION is a violation
#  of the copyright laws and may subject you to criminal prosecution.
# 
platform_os=`uname -s`

if [ "$platform_os" = "Linux" ];then
    # for rhel5 and SLES10 SP1 installs, lpfcdfc does not exist
    ioctl_driver="lpfcdfc"

    if [ -f /etc/redhat-release ]; then
       rhel_major_version=`cat /etc/redhat-release | awk '{ print $7 }' | awk -F. '{ print $1 }'`

       if [ "$rhel_major_version" -eq 4 ];then
           # is this rhel5 Tikanga release
           rhel5_check=`cat /etc/redhat-release | grep Tikanga`
           if [ -n "$rhel5_check" ];then
               ioctl_driver="lpfc"
           fi
       else
           ioctl_driver="lpfc"
       fi
    elif [ -f /etc/SuSE-release ]; then
       # Check for SLES10 SP1
       sles_major_version=`cat /etc/SuSE-release | grep VERSION | awk '{ print $3 }'`
       if [ "$sles_major_version" -eq 10 ]; then
           sles_minor_version=`cat /etc/SuSE-release | grep PATCHLEVEL | awk '{ print $3 }'`
           if [ -n "$sles_minor_version" ];then
               ioctl_driver="lpfc"
           fi
       fi
    fi
fi

#  If HBAnyware running, then tell user to exit it before continuing
pid=`ps -eaf | grep HBAnyware.jar | grep -v grep | grep -v start | awk '{ print $2 }' | sort -n | head -1`
if [ ! -z "$pid" ];then
    echo " "
    echo "The HBAnyware utility is currently running.  Before starting"
    echo "Web Launch Services, you must first stop the HBAnyware utility"
    echo "by performing the following steps:"
    echo " "
    echo "  - Exit the HBAnyware utility"
    echo "  - Stop all HBAnyware services by executing the shell"
    echo "    script '/usr/sbin/hbanyware/stop_hbanyware -n'"
    echo "  - Re-execute this script (start_weblaunch)"
    echo " "
    exit -1
fi

# If web launch is not installed, then don't do anything
if [ ! -f /usr/sbin/hbanyware/document_root ]; then
    echo "Web Launch Services currently are not installed!"
    exit -1
fi

#  Kill parent rmiserver
ppid=`ps -eaf | grep rmiserver | grep -v grep | grep -v start | awk '{ print $2 }' | sort -n | head -1`
if [ ! -z "$ppid" ];then
    kill $ppid > /dev/null 2>&1
fi

#  Kill any zombied rmiserver children
for ppid in `ps -eaf | grep rmiserver | grep -v grep | grep -v start | awk '{ print $2 }' | sort -n`
do
    kill -9 $ppid > /dev/null 2>&1
done

# If 'All' command line option is specified, then kill rmiregistry as well
if [ "$1" = "-all" ]; then
    ppid=`ps -eaf | grep rmiregistry | grep -v grep | awk '{ print $2 }' | sort -n`
    if [ ! -z "$ppid" ];then
        kill -9 $ppid > /dev/null 2>&1
    fi
fi

if [ "$platform_os" = "Linux" ];then
    #  Start the server daemon if module loaded
    lpfcdfc_loaded=`lsmod | awk '{ print $1 }' | grep "$ioctl_driver"`
    if [ ! -z "$lpfcdfc_loaded" ];then
        if [ -f /usr/sbin/hbanyware/rmiserver ]; then
            ppid=`ps -ea | grep rmiregistry | grep -v grep | grep -v start | awk '{ print $1 }' | sort -n | head -1`
            if [ -z "$ppid" ];then
              /usr/sbin/hbanyware/jre/bin/rmiregistry &
            fi
	    sleep 1 
            /usr/sbin/hbanyware/rmiserver &
        fi
    else
        echo ""
        echo "Not starting rmiserver daemon because $ioctl_driver driver module not loaded."
        echo "Load $ioctl_driver driver (e.g. modprobe $ioctl_driver) and retry."
    fi
else
    ppid=`ps -eaf | grep rmiserver | grep -v grep | grep -v start | awk '{ print $2 }' | sort -n | head -1`
    if [ -z "$ppid" ];then
      rmiregistry &
    fi
    sleep 1 
    /usr/sbin/hbanyware/rmiserver &
fi
